package com.webserver.controller;

import com.webserver.core.ClientHandler;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * MVC模型
 * M:model
 * V:view
 * C:controller
 * 处理与用户相关的操作
 */
public class UserController {

    private static File USER_DIR = new File("./users");

    private static File staticDir;
    static {
        if (!USER_DIR.exists()){
            USER_DIR.mkdirs();
        }

        try {
            staticDir = new File(
                    ClientHandler.class.getClassLoader().getResource(
                            "./static"
                    ).toURI()
            );
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    /**
     * 处理用户注册
     * @param request
     * @param response
     */
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户注册...");

        //1获取用户表单提供上来的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);

        if (username==null||password==null||nickname==null||ageStr==null||
        !ageStr.matches("[0-9]+")){
            File file = new File(staticDir,"/myweb/reg_fail.html");
            response.setContentFile(file);
            return;
        }

        //2将用户信息以一个User实例形式表示，并序列化到文件里
        int age = Integer.parseInt(ageStr);
        User user = new User(username,password,nickname,age);
        //将改用户信息以户名.obg的形式保存到user目录里
        File userFile = new File(USER_DIR,username+".obj");
        if (userFile.exists()){
            File file = new File(staticDir,"/myweb/have_user.html");
            response.setContentFile(file);
            return;
        }
        try(
                FileOutputStream fos = new FileOutputStream(userFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(user);
            //注册成功了
            File file = new File(staticDir,"/myweb/reg_success.html");
            response.setContentFile(file);
        } catch (IOException e){

        }

        System.out.println("处理用户注册完毕");
    }

    public void login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始登录。。");
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        if (username==null||password==null) {
            File file = new File(staticDir,"/myweb/login_info_error.html");
            response.setContentFile(file);
            return;
        }

        File userFile = new File(USER_DIR,username+".obj");
        if (userFile.exists()){
            try(
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ){
                User user =  (User) ois.readObject();
                if (user.getPassword().equals(password)){
                    File file = new File(staticDir,"/myweb/login_success.html");
                    response.setContentFile(file);
                    return;
                }
            }catch (IOException | ClassNotFoundException e){

            }
        }
        File file = new File(staticDir,"/myweb/login_fail.html");
        response.setContentFile(file);
        System.out.println("登陆成功");
    }

    /**
     * 生成显示所有用户的动态页面
     * @param request
     * @param response
     */
    public void showAllUser(HttpServletRequest request,HttpServletResponse response) {
        System.out.println("开始生成动态页面...");
        //把users里的文件全部读出来，然后存入list集合
        List<User> userList = new ArrayList<>();
        //先将users下所有的.obj文件获取回来
        File dir = new File("./users");
        File[] files = dir.listFiles(
                f->f.getName().endsWith(".obj")
        );
        for (int i = 0; i < files.length; i++) {
            try (
                    FileInputStream fis  = new FileInputStream(files[i]);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    )
            {
                User user = (User)ois.readObject();
                userList.add(user);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
        //先将users下所有的.obj文件获取回来
//        File[] subs = USER_DIR.listFiles(f->f.getName().endsWith(".obj"));
//        //遍历每一个obj文件并进行反序列化得到user
//        for (File userFiles:subs) {
//            try (
//                    FileInputStream fis  = new FileInputStream(userFiles);
//                    ObjectInputStream ois = new ObjectInputStream(fis);
//                    ){
//                User user = (User) ois.readObject();
//                //将user对象存入userList集合备用
//                userList.add(user);
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//
//        }

        File file = new File("./userList.html");

        try(
                PrintWriter pw = new PrintWriter(file);
                ) {
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>用户名</td>");
            pw.println("<td>密码</td>");
            pw.println("<td>昵称</td>");
            pw.println("<td>年龄</td>");
            pw.println("</tr>");

            for (User user : userList) {
                pw.println("<tr>");
                pw.println("<td>"+user.getUsername()+"</td>");
                pw.println("<td>"+user.getPassword()+"</td>");
                pw.println("<td>"+user.getNickname()+"</td>");
                pw.println("<td>"+user.getAge()+"</td>");
                pw.println("</tr>");
            }

            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");

        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        response.setContentFile(file);

        System.out.println("动态页面生成成功");
    }

}
